/*
 * Copyright (c) 2020, Peter Abeles. All Rights Reserved.
 *
 * This file is part of BoofCV (http://boofcv.org).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package boofcv.alg.segmentation.fh04.impl;

import boofcv.alg.segmentation.fh04.FhEdgeWeights;
import boofcv.struct.image.GrayF32;
import boofcv.struct.image.ImageType;
import boofcv.struct.image.Planar;
import org.ddogleg.struct.DogArray;

import static boofcv.alg.segmentation.fh04.SegmentFelzenszwalbHuttenlocher04.Edge;

/**
 * <p>Computes edge weight as the F-norm different in pixel value for {@link Planar} images.
 * A 8-connect neighborhood is considered.</p>
 *
 * <p>
 * DO NOT MODIFY. This code was automatically generated by GenerateFhEdgeWeights_PL.
 * <p>
 *
 * @author Peter Abeles
 */
public class FhEdgeWeights8_PLF32 implements FhEdgeWeights<Planar<GrayF32>> {

	float[] pixelColor = new float[1];

	@Override
	public void process(Planar<GrayF32> input, DogArray<Edge> edges) {
		if( pixelColor.length != input.getNumBands() ) {
			pixelColor = new float[ input.getNumBands() ];
		}
		final int numBands = pixelColor.length;

		edges.reset();
		int w = input.width-1;
		int h = input.height-1;

		// First consider the inner pixels
		for( int y = 0; y < h; y++ ) {
			int indexSrc = input.startIndex + y*input.stride + 1;
			int indexDst =                  + y*input.width  + 1;

			for( int x = 1; x < w; x++ , indexSrc++ , indexDst++ ) {

				float weight1=0,weight2=0,weight3=0,weight4=0;

				for( int i = 0; i < numBands; i++ ) {
					GrayF32 band = input.getBand(i);

					float color0 = band.data[indexSrc];                       // (x,y)
					float color1 = band.data[indexSrc+1];                     // (x+1,y)
					float color2 = band.data[indexSrc+input.stride];          // (x,y+1)
					float diff1 = color0-color1;
					float diff2 = color0-color2;
					weight1 += diff1*diff1;
					weight2 += diff2*diff2;
					float color3 = band.data[indexSrc+1+input.stride];        // (x+1,y+1)
					float color4 = band.data[indexSrc-1+input.stride];        // (x-1,y+1)
					float diff3 = color0-color3;
					float diff4 = color0-color4;
					weight3 += diff3*diff3;
					weight4 += diff4*diff4;
				}

				Edge e1 = edges.grow();
				Edge e2 = edges.grow();

				e1.sortValue = (float)Math.sqrt(weight1);
				e1.indexA = indexDst;
				e1.indexB = indexDst+1;

				e2.sortValue = (float)Math.sqrt(weight2);
				e2.indexA = indexDst;
				e2.indexB = indexDst+input.width;

				Edge e3 = edges.grow();
				Edge e4 = edges.grow();

				e3.sortValue = (float)Math.sqrt(weight3);
				e3.indexA = indexDst;
				e3.indexB = indexDst+1+input.width;

				e4.sortValue = (float)Math.sqrt(weight4);
				e4.indexA = indexDst;
				e4.indexB = indexDst-1+input.width;
			}
		}

		// Handle border pixels
		for( int y = 0; y < h; y++ ) {
			checkAround(0,y,input,edges);
			checkAround(w,y,input,edges);
		}

		for( int x = 0; x < w; x++ ) {
			checkAround(x,h,input,edges);
		}
	}

	private void checkAround( int x , int y ,
							  Planar<GrayF32> input ,
							  DogArray<Edge> edges )
	{
		int indexSrc = input.startIndex + y*input.stride + x;
		int indexA =                      y*input.width  + x;

		final int numBands = pixelColor.length;

		for( int i = 0; i < numBands; i++ ) {
			GrayF32 band = input.getBand(i);
			pixelColor[i] =  band.data[indexSrc];
		}

		check(x+1, y, pixelColor,indexA,input,edges);
		check(x  ,y+1,pixelColor,indexA,input,edges);
		check(x+1,y+1,pixelColor,indexA,input,edges);
		check(x-1,y+1,pixelColor,indexA,input,edges);
	}

	private void check( int x , int y , float color0[] , int indexA,
						Planar<GrayF32> input ,
						DogArray<Edge> edges ) {
		if( !input.isInBounds(x,y) )
			return;

		int indexSrc = input.startIndex + y*input.stride + x;
		int indexB   =                  + y*input.width  + x;

		float weight = 0;

		final int numBands = pixelColor.length;

		for( int i = 0; i < numBands; i++ ) {
			GrayF32 band = input.getBand(i);

			float color = band.data[indexSrc];
			float diff = color0[i]-color;
			weight += diff*diff;
		}

		Edge e1 = edges.grow();

		e1.sortValue = (float)Math.sqrt(weight);
		e1.indexA = indexA;
		e1.indexB = indexB;
	}

	@Override
	public ImageType<Planar<GrayF32>> getInputType() {
		return ImageType.pl(3,GrayF32.class);
	}

}
